import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class LoginPanel extends JPanel {
	private JButton login, forgotPassword, retrievePassword, cancel;
	private JTextField user, email;
	private JPasswordField pass;
	private JLabel luser, lpass, lemail, header1, header2, header3, loginboxheader, fpboxheader;
	private JPanel loginBox, loginEntries, loginButtons, fpBox, fpButtons, fpEntry, mainBox;
	public LoginPanel(int length, int width) {
		//---- OBJECT INITIALIZATION ----//
		login = new JButton("Login");
		forgotPassword = new JButton("Forgot Password?");
		retrievePassword = new JButton("Retrieve Password");
		cancel = new JButton("Cancel");
		user = new JTextField("");
		email = new JTextField("");
		pass = new JPasswordField("");

		// implement Font later

		luser = new JLabel("Username: ");
		lpass = new JLabel("Password: ");
		lemail = new JLabel("Email: ");
		header1 = new JLabel("TeamFive");
		header2 = new JLabel("Medical Assistant");
		header3 = new JLabel("Mercy Hospital");
		loginboxheader = new JLabel("Login");
		fpboxheader = new JLabel("Forgot Password?");

		//---- LOGIN PANEL ----//
		// Username/Password Entries
		loginEntries = new JPanel();
		loginEntries.setLayout(new GridLayout(2, 2));
		loginEntries.add(luser);
		loginEntries.add(user);
		loginEntries.add(lpass);
		loginEntries.add(pass);

		// Login/Forgot Password Buttons
		loginButtons = new JPanel();
		loginButtons.add(login);
		loginButtons.add(forgotPassword);

		// Login Box
		loginBox = new JPanel();
		loginBox.setLayout(new BorderLayout());
		loginBox.add(loginboxheader, BorderLayout.NORTH);
		loginBox.add(loginEntries, BorderLayout.CENTER);
		loginBox.add(loginButtons, BorderLayout.SOUTH);

		//---- FORGOT PASSWORD PANEL ----//
		// Email Entry
		fpEntry = new JPanel();
		fpEntry.setLayout(new GridLayout(1, 2));
		fpEntry.add(lemail);
		fpEntry.add(email);

		// Send Email/Never Mind Buttons
		fpButtons = new JPanel();
		fpButtons.add(retrievePassword);
		fpButtons.add(cancel);

		// Forgot Password Box
		fpBox = new JPanel();
		fpBox.setLayout(new BorderLayout());
		fpBox.add(fpboxheader, BorderLayout.NORTH);
		fpBox.add(fpEntry, BorderLayout.CENTER);
		fpBox.add(fpButtons, BorderLayout.SOUTH);

		//---- COMBINE LOGIN/FORGOT PASSWORD PANELS ----//
		mainBox = new JPanel();
		mainBox.setBorder(BorderFactory.createLineBorder(Color.black));
		mainBox.setLayout(new CardLayout());
		mainBox.add(loginBox, "Login");
		mainBox.add(fpBox, "Forgot Password?");

		//---- MAIN LAYOUT ----//
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		add(header1);
		header1.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(header2);
		header2.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(Box.createRigidArea(new Dimension(0, 5)));
		add(header3);
		header3.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(Box.createRigidArea(new Dimension(0, 100)));
		add(mainBox);
		mainBox.setAlignmentX(Component.CENTER_ALIGNMENT);

		// Button Listeners
		ButtonListener lis = new ButtonListener();
		login.addActionListener(lis);
		forgotPassword.addActionListener(lis);
		retrievePassword.addActionListener(lis);
		cancel.addActionListener(lis);

	}
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			Object action = event.getSource();
			CardLayout cLayout = (CardLayout) (mainBox.getLayout());
			if (action == login) {
				// LOGIN ACTION
				// Should check for correct username/password combination.
				// (For now, use a placeholder)
			}
			else if (action == forgotPassword) {
				user.setText("");
				pass.setText("");
				cLayout.show(mainBox, "Forgot Password?");
			}
			else if (action == retrievePassword) {
				// RETRIEVE PASSWORD ACTION
				// Should send the user an email
			}
			else if (action == cancel) {
				cLayout.show(mainBox, "Login");
			}
		}
	}
}